﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace Housing
{
    public class StreetCheck
    {
        private int _str_id;
        private int _str_noid;
        private string _str_showtime;
        private string _str_isconflict;
        private string _str_checktime;
        private string _str_signature;
        private string _str_responsname;
        private string _str_applyid;

        public string Str_applyid
        {
            get { return _str_applyid; }
            set { _str_applyid = value; }
        }

        public int str_id
        {
            get { return _str_id; }
            set { _str_id = value; }
        }
        public int str_noid
        {
            get { return _str_noid; }
            set { _str_noid = value; }
        }
        public string str_showtime
        {
            get { return _str_showtime; }
            set { _str_showtime = value; }
        }
        public string str_isconflict
        {
            get { return _str_isconflict; }
            set { _str_isconflict = value; }
        }
        public string str_checktime
        {
            get { return _str_checktime; }
            set { _str_checktime = value; }
        }
        public string str_signature
        {
            get { return _str_signature; }
            set { _str_signature = value; }
        }
        public string str_responsname
        {
            get { return _str_responsname; }
            set { _str_responsname = value; }
        }

        public StreetCheck()
        {
        }

        public StreetCheck(int strId)
        {
            GetStreetCheckByStrId(strId);
        }

        public void GetStreetCheckByStrId(int strId)
        {
            string query = "SELECT * FROM StreetCheck WHERE str_id=" + strId;

            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, CommandType.Text, query);
            if (dr.Read())
            {
                _str_id = (int)dr[0];
                _str_noid = (int)dr[1];
                _str_showtime = Utils.GetString(dr[2]);
                _str_isconflict = Utils.GetString(dr[3]);
                _str_checktime = Utils.GetString(dr[4]);
                _str_signature = Utils.GetString(dr[5]);
                _str_responsname = Utils.GetString(dr[6]);
            }

            dr.Close();
        }

        public int AddNewStreetCheck(StreetCheck str)
        {
            string query = "INSERT INTO StreetCheck(str_noid, str_showtime, str_isconflict, str_checktime, str_signature, str_responsname,applyid)"
                + "VALUES(" + str.str_noid + ",'" + str.str_showtime + "','" + str.str_isconflict + "','" + str.str_checktime + "','" + str.str_signature + "','" + str.str_responsname + "', " + str.Str_applyid + ")";

            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, query);
        }

        public DataSet GetAllStreetCheck()
        {
            string query = "SELECT * FROM StreetCheck";
            return SqlHelper.ExecuteDataset(SqlHelper.ConnectionString, CommandType.Text, query);
        }

        public int UpdateStreetCheckByStrId(StreetCheck str)
        {
            string query = "UPDATE StreetCheck SET str_noid=" + str.str_noid + ",str_showtime='" + str.str_showtime + "',"
                + "str_isconflict='" + str.str_isconflict + "', str_checktime='" + str.str_checktime + "',"
                + "str_signature='" + str.str_signature + "',str_response='" + str.str_responsname + "'"
                + "WHERE str_id=" + str.str_id;

            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, query);
        }

        public int DeleteStreetCheckByStrId(int strId)
        {
            string query = "DELETE FROM StreetCheck WHERE str_id=" + strId;
            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, query);
        }

        public int AddStreetPublishCheck(string applyid, string pubdate, string ispro)
        {
            string cmd = "SELECT COUNT(*) FROM StreetCheck WHERE applyid=" + applyid;
            int succornot = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, CommandType.Text, cmd));
            if (succornot == 0)
            {
                StreetCheck streetcheck = new StreetCheck();
                streetcheck._str_noid = 0;
                streetcheck._str_showtime = pubdate;
                streetcheck._str_isconflict = ispro;
                streetcheck._str_checktime = "";
                streetcheck._str_responsname = "";
                streetcheck._str_signature = "";
                streetcheck._str_applyid = applyid;
                int succ=streetcheck.AddNewStreetCheck(streetcheck);
                return succ;
            }
            else
                return 0;
        }

        public bool AddStreeCheckUpd(string applyid, string name, string responname, string time)
        {
            string query = "SELECT str_signature FROM StreetCheck WHERE applyid=" + applyid + " AND applyid IN(SELECT applyid FROM Apply WHERE apply_passed=2)";
            string str = Convert.ToString(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, CommandType.Text, query));
            if (str == "")
            {
                StreetCheck streetcheck = new StreetCheck();

                string cmdtext = "UPDATE StreetCheck SET str_signature='" + name + "',str_responsname='" + responname + "',str_checktime='" + time + "' WHERE applyid=" + applyid;
                bool succ = Convert.ToBoolean(SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, cmdtext));
                return succ;
            }
            else
                return false;
        }
    }
}
